<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="input-group">
                <span class="input-group-append">
                    <span class="input-group-text">粘贴公网链接</span>
                </span>
                <input id="txtUrl" type="text" class="form-control" placeholder="请粘贴公网文档链接地址">
                <span class="input-group-append">
                    <span class="input-group-text">上传Office文档（Max 20MB）</span>
                </span>
                <div class="custom-file">
                    <input id="txtFile" type="file" class="custom-file-input">
                    <label class="custom-file-label" id="ps">选择（拖拽）本地 Word、Excel、PPT</label>
                </div>
            </div>
            <p>
                <label>全屏完整链接：</label>
                <a target="_blank" id="auri" href="javascript:void(0)">https://view.officeapps.live.com/op/embed.aspx?src=</a>
            </p>
        </div>
        <div class="col-md-12">
            <iframe src="about:blank" class="d-none" frameborder="0"></iframe>
        </div>
    </div>
</div>

<script>
    var ov = {
        api: "https://view.officeapps.live.com/op/embed.aspx?src=",
        init: function () {

            $('#ps').attr('tip', $('#ps').html());

            //接收文件
            ss.receiveFiles(function (files) {
                ov.upload(files[0]);
            }, "#txtFile");

            $('#txtUrl').on('input', function () {
                ov.view(this.value);
            });

            $(window).on('load resize', function () {
                ov.size();
            });
        },
        view: function (url) {
            if (url && url != "") {
                url = decodeURIComponent(url);
                $('#auri').attr('href', ov.api + url);
                $('#auri').html(ov.api + url);
                $('iframe').removeClass("d-none").attr('src', ov.api + url);
            } else {
                $('#auri').attr('href', 'javascript:void(0);');
                $('#auri').html(ov.api);
                $('iframe').addClass("d-none").attr('src', 'about:blank');
            }
            ov.size();
        },
        size: function () {
            var h = $(window).height() - $('iframe').offset().top - 15;
            $('iframe').height(Math.max(200, h)).width('100%');
        },
        upload: function (file) {
            var err = [];
            if (file.size > 1024 * 1024 * 20) {
                err.push('文档大小限制 20MB')
            }
            if (file.type.indexOf('application') == -1 || ".doc docx .xls xlsx .ppt pptx".indexOf(file.name.slice(-4).toLowerCase()) == -1) {
                err.push('请选择 Office文档')
            }
            if (err.length) {
                jz.alert(err.join('<br/>'))
            } else {
                //上传
                var formData = new FormData();
                formData.append("file", file);

                $('#ps').html('正在准备上传 ');
                $.ajax({
                    url: "https://www.netnr.eu.org/api/v1/Upload",
                    type: "POST",
                    data: formData,
                    contentType: false,
                    processData: false,
                    dataType: 'json',
                    xhr: function () {
                        xhr = $.ajaxSettings.xhr();
                        xhr.upload.addEventListener('progress', function (e) {
                            var rate = ((e.loaded / e.total) * 100).toFixed();
                            if (rate < 100) {
                                $('#ps').html('已上传 ' + rate + ' %');
                            } else {
                                $('#ps').html($('#ps').attr('tip'));
                            }
                        })
                        return xhr;
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.code == 200) {
                            ov.view("https://www.netnr.eu.org" + data.data.path);
                        } else {
                            jz.alert(data.msg)
                        }
                    },
                    error: function () {
                        jz.alert("上传出错");
                    },
                    complete: function () {
                        $('#txtFile').val('');
                        $('#ps').html($('#ps').attr('tip'));
                    }
                });
            }
        }
    }

    ov.init();
</script>